<?php
namespace app\common\controller;

use \thans\Ret;
use \think\Controller;
use \think\Loader;
use \think\Request;
use \think\Session;

/**
 *
 */
class Base extends Controller
{

    public $now_uri;
    public $now_url;
    public $ret;
    public function __construct()
    {
        $this->request = Request::instance();
        parent::__construct();
        session('body_id', 0);
        $this->now_uri = $this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action();
        $this->now_uri = strtolower($this->now_uri);
        // var_dump($_GET);
        $this->now_url = $this->request->path();
        // var_dump($param);
        $this->assign('base_uri', url($this->now_uri));
        $this->assign('now_uri', $this->now_uri);
        $this->assign('now_url', $this->now_url);
        //$this->now_url=$this->request->url();
        $this->bodymap['body_id'] = session('body_id');
        // $this->checkAuth();
        $this->ret = Ret::i();
        $this->log();
        $this->options = Loader::model('options');
        /*
        自动从数据库中加载配置项。
         */
        $this->auto_load_options();
    }

    /**
     * 记录操作日志
     */
    public function log()
    {
        $log               = Loader::model('ActionsLog');
        $data['log_m']     = $this->request->module();
        $data['log_c']     = $this->request->controller();
        $data['log_a']     = $this->request->action();
        $data['log_value'] = $this->now_url;
        if (Request::instance()->isGet()) {
            $data['log_type'] = 'GET';
        }
        if (Request::instance()->isPost()) {
            $data['log_type'] = 'POST';
            define('IS_POST', true);
        } else {
            define('IS_POST', false);
        }
        if (Request::instance()->isPut()) {
            $data['log_type'] = 'PUT';
        }
        if (Request::instance()->isAjax()) {
            $data['log_type'] = 'Ajax';
        }
        if (Request::instance()->isPjax()) {
            $data['log_type'] = 'Pjax';
        }
        if (Request::instance()->isHead()) {
            $data['log_type'] = 'HEAD';
        }
        if (Request::instance()->isPatch()) {
            $data['log_type'] = 'PATCH';
        }
        if (Request::instance()->isCli()) {
            $data['log_type'] = 'cli';
        }
        if (Request::instance()->isCgi()) {
            $data['log_type'] = 'cgi';
        }
        $data['log_time'] = date('Y-m-d H:i:s', time());

        $data['log_uid'] = intval(session('u_id'));

        $data['log_ip'] = inge_get_ip();

        $data['log_os']      = inge_get_os();
        $data['log_browser'] = inge_get_browser() . ' ' . inge_get_browser_ver();

        $body['header']   = $this->request->header();
        $body['body']     = input('');
        $data['log_body'] = json_encode($body);

        // var_dump($data);die();
        $log->insert($data);
    }
    public function auto_load_options()
    {
        $res = $this->options->where('option_autoload', '1')->select();
        foreach ($res as $key => $value) {
            $options               = json_decode($value['option_value'], true);
            if (is_array($options)) {
                foreach ($options as $k => $v) {
                    $this->assign($k,$v);
                }
            }else{
                $this->assign($value['option_name'],$options);
            }
        }
    }

}
